package com.cowin.inf.service.impl;

import com.alibaba.fastjson.JSON;
import com.cowin.base.result.Result;
import com.cowin.base.result.ResultGenerator;
import com.cowin.inf.dao.KyLineStateInfoMapper;
import com.cowin.inf.dao.KyLineStateMapper;
import com.cowin.inf.model.KyLineState;
import com.cowin.inf.model.KyLineStateInfo;
import com.cowin.inf.service.KyLineStateService;
import com.cowin.base.AbstractServiceImpl;

import lombok.extern.slf4j.Slf4j;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

import java.util.List;


/**
 * Created by  on 2019/09/19.
 *
 * @author CodeGenerator
 */
@Slf4j
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class KyLineStateServiceImpl extends AbstractServiceImpl<KyLineState> implements KyLineStateService {


    @Resource
    private KyLineStateMapper kyLineStateMapper;


    @Resource
    private KyLineStateInfoMapper kyLineStateInfoMapper;



    @Override
    public Result lineStateMonitor(KyLineState kyLineState) {
        List<KyLineState> lineStateList = kyLineStateMapper.selectByPlantWorkShopLineNo(kyLineState.getPlantNo(), kyLineState.getWorkShopNo(), kyLineState.getLineNo());

        if (!CollectionUtils.isEmpty(lineStateList)) {
            KyLineState selectKyLineState= lineStateList.get(0);
            if(!selectKyLineState.getStatus().equals(kyLineState.getStatus())){
                KyLineStateInfo kyLineStateInfo = new KyLineStateInfo();
                kyLineStateInfo.setPlantNo(kyLineState.getPlantNo());
                kyLineStateInfo.setWorkShopNo(kyLineState.getWorkShopNo());
                kyLineStateInfo.setLineNo(kyLineState.getLineNo());
                kyLineStateInfo.setStartTime(selectKyLineState.getNodeTime());
                kyLineStateInfo.setEndTime(kyLineState.getNodeTime());
                kyLineStateInfo.setDelay(Integer.parseInt((kyLineState.getNodeTime().getTime() - selectKyLineState.getNodeTime().getTime()) / 60000 + ""));
                kyLineStateInfo.setStatus(selectKyLineState.getStatus());
                kyLineStateInfo.setBreakReason(selectKyLineState.getBreakReason());
                selectKyLineState.setStatus(kyLineState.getStatus());
                selectKyLineState.setNodeTime(kyLineState.getNodeTime());
                selectKyLineState.setBreakReason(kyLineState.getBreakReason());
                kyLineStateMapper.updateByPrimaryKeySelective(selectKyLineState);
                kyLineStateInfoMapper.insertSelective(kyLineStateInfo);
                       }
        } else {
            kyLineStateMapper.insertSelective(kyLineState);
               }
        return ResultGenerator.genSuccessResult(JSON.toJSONString(kyLineState));
    }

    @Override
    public List<KyLineState> initLineState(String plantNo, String workShopNo) {
        return kyLineStateMapper.initLineState(plantNo, workShopNo);
    }

    @Override
    public KyLineState getLineState(String workShopNo, String lineNo) {
        return kyLineStateMapper.getLineState(workShopNo,lineNo);
    }
}
